package com.qianfeng.wuyang.himalaya.adapter.recommendadapter;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.widget.Toast;

import com.qianfeng.wuyang.himalaya.bean.recommendbean.HottestTopEntity;
import com.qianfeng.wuyang.himalaya.bean.recommendbean.TodayHottestCategoryEntity;
import com.qianfeng.wuyang.himalaya.callback.recommendcallback.TopHottestService;
import com.qianfeng.wuyang.himalaya.fragment.TodayHottestViewpagerFragment;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 *
 * Created by WangQing on 2016/12/11.
 */

public class TodayHottestAdapter extends FragmentPagerAdapter implements Callback<HottestTopEntity> {
    private List<TodayHottestCategoryEntity> categoryEntities = new ArrayList<>();
    private Context context;
    private String key;

    public TodayHottestAdapter(FragmentManager fm, Context context,String key) {
        super(fm);
        this.context = context;
        this.key = key;
//        this.categoryEntities = categoryEntities;
        TopHottestService service = new Retrofit.Builder()
                .baseUrl("http://mobile.ximalaya.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(TopHottestService.class);
        service.getTopHottest("android",key,1,20,2).enqueue(this);

    }


    @Override
    public Fragment getItem(int position) {

        return TodayHottestViewpagerFragment.newInstance();
    }

    @Override
    public int getCount() {
        return categoryEntities.size();
    }



    @Override
    public CharSequence getPageTitle(int position) {

        return categoryEntities.get(position).getName();
    }

    @Override
    public void onResponse(Call<HottestTopEntity> call, Response<HottestTopEntity> response) {

        if (response.body().getCategories()!=null && categoryEntities.size()==0) {
            categoryEntities.addAll(response.body().getCategories());
        }
        notifyDataSetChanged();//

    }

    @Override
    public void onFailure(Call<HottestTopEntity> call, Throwable t) {
        t.printStackTrace();
        Toast.makeText(context,t.getMessage(),Toast.LENGTH_SHORT).show();
    }
}
